<?php
/**
 +------------------------------------------------------------------------------
 * 公共方法
 +------------------------------------------------------------------------------
 * @author    木頭
 * @version   1.2
 * @filename functions.php
 * @date 2009/12/17
 * @last modified 2010.4.12
 +------------------------------------------------------------------------------
 */
if(! defined('APP_PATH')) exit;


/**
 +----------------------------------------------------------
 * url装配车间
 +----------------------------------------------------------
 * @param string $base 控制器和方法
 * @param array $others url其他参数
 +----------------------------------------------------------
 * @return string url 组装后的url
 +----------------------------------------------------------
 */
function url($base,$others='')
{
	$others_url='';
	if(is_array($others))
	{
		foreach ($others as $k=>$v)
		{
			$others_url.='/'.$k.'/'.$v;
		}

	}
	if(defined('HTML_URL_SUFFIX')) $url_suffix = HTML_URL_SUFFIX;
	else $url_suffix = '';


	// sid手机加密串，sid加密在login中生成。
	if(!empty($_GET['sid']) && ($_GET['sid'] != 'guest'))
		$sid = '/sid/'.$_GET['sid'];
	else
	{// 调试过程中发现一个奇怪的现象，cookie比实际情况要慢一拍，比如以已经撤销了cookie，那么当次cookie还是有值的，下次才处理掉。
	 // 这样会影响到退出的生效问题。
		// 如果cookie在，则用cookie中的手机号
		if(!empty($_COOKIE['phone']))
		{
			$_GET['phone']=(int)uncryptPhone($_COOKIE['phone']);

			// 验证手机号的合法性，如果不合法，则sid=guest
			if( strlen($_GET['phone']) == 11)
			{
				$sid = '/sid/'.cryptPhone($_GET['phone']);
			}
			else
			{
				$sid = '/sid/guest';
			}

		}
		else
		{
			$sid = '/sid/guest';
		}
	}

	return URL.'/'.$base . $sid . $others_url . $url_suffix;
}

/**
 +----------------------------------------------------------
 * 提示成功信息，然后跳转
 +----------------------------------------------------------
 +----------------------------------------------------------
 * @return void
 +----------------------------------------------------------
 */
function success($msg,$url='')
{
	$view = new View();
	if(empty($url))
		$url=empty($_SERVER['referer'])?$_SERVER['REQUEST_URI']:$_SERVER['referer'];


	$view->assign('msg',$msg);
	$view->assign('url',$url);
	$view->assign('wait',3);

	$view->render('public/msg.php');
	exit;
}

/**
 +----------------------------------------------------------
 * 显示错误信息,然后跳转
 +----------------------------------------------------------
 +----------------------------------------------------------
 * @return void
 +----------------------------------------------------------
 */
function error($msg,$url='')
{
	$view = new View();
	if(empty($url))
		$url=empty($_SERVER['referer'])?$_SERVER['REQUEST_URI']:$_SERVER['referer'];


	$view->assign('msg','<span style="color:red;">'.$msg.'</span>');
	$view->assign('url',$url);
	$view->assign('wait',3);

	$view->render('public/msg.php');
	exit;
}
/**
 +----------------------------------------------------------
 * 跳转到指定url
 +----------------------------------------------------------
 * @param string $base 控制器和方法
 * @param array $others url其他参数
 +----------------------------------------------------------
 * @return void
 +----------------------------------------------------------
 */
function redirect($base,$others='')
{
	$view = new View();

	$view->assign('msg','');
	$view->assign('url', url($base,$others));
	$view->assign('wait',0);

	$view->render('public/msg.php');
	exit;
}




// 手机号码加密方法 2010-6-17 guan
function cryptPhone($phone)
{
	$original = base64_encode($phone);
	$arr0 = array('A','b','C','c','D','E','e','Z','5','9','G','l','l','4','m','B','V');
	$arr1 = array('A','b','C','c','D','E','8','4','m','B','Z','5','9','G','l','l','V');
	$arr2 = array('C','b','C','c','R','T','e','Z','5','9','G','l','l','4','m','B','V');
	$arr3 = array('A','O','C','c','D','E','e','Z','5','9','G','l','l','4','m','B','p');
	$arr4 = array('S','b','C','H','D','E','e','Z','5','9','G','l','l','4','m','B','V');
	shuffle($arr0);
	shuffle($arr1);
	shuffle($arr2);
	shuffle($arr3);
	shuffle($arr4);

	return substr($original, -5,4).$arr0[1].$arr1[2].$arr2[3].$arr3[4].$arr4[5].substr($original,0,-5).substr($original, -1);
}

// 手机号码解密方法 2010-6-17 guan
function uncryptPhone($crypted_str)
{
	if ($crypted_str=='guest') return 'guest';

	$sb = substr($crypted_str, 9,-1).substr($crypted_str,0,4).substr($crypted_str,-1);
	return base64_decode($sb);
}

/* End of file functions.php */
/* Location: exts/functions.php */